package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.BaseContext_id获取;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart_购物车;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: cl
 * @Date: 2022/4/8 - 04 - 08
 * @Description:
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 展示购物车
     *
     * @param shoppingCart
     * @return
     */
    @GetMapping("/list")
    private R<List<ShoppingCart_购物车>> list(ShoppingCart_购物车 shoppingCart) {
        List<ShoppingCart_购物车> list = shoppingCartService.list(
                Wrappers.<ShoppingCart_购物车>lambdaQuery()
                        .eq(ShoppingCart_购物车::getUserId, BaseContext_id获取.getCurrentId())
                        .orderByDesc(ShoppingCart_购物车::getCreateTime)
        );

        return R.success(list);
    }

    /**
     * 添加购物车商品
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    private R<ShoppingCart_购物车> add(@RequestBody ShoppingCart_购物车 shoppingCart) {
        log.info(shoppingCart.toString());

        //1、设定购物车用户得id  分类是哪个用户添加的商品
        shoppingCart.setUserId(BaseContext_id获取.getCurrentId());

        //方式一：
//        //2、分类用户添加的是套餐还是菜品，
//        Long dishId = shoppingCart.getDishId();
//
//        //3、创建套餐和菜品共用的LambdaQueryWrapper (都需要等于userId)还有各自的套餐或者菜品id
//        LambdaQueryWrapper<ShoppingCart_购物车> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart_购物车::getUserId, shoppingCart.getUserId());
//
//        if (dishId != null) {
//            queryWrapper.eq(ShoppingCart_购物车::getDishId, shoppingCart.getDishId());
//        } else {
//            queryWrapper.eq(ShoppingCart_购物车::getSetmealId, shoppingCart.getSetmealId());
//        }
//
//        //4、查询此菜品或者套餐是否存在，不存在就是第一次买，存在就把数量加1
//        ShoppingCart_购物车 one = shoppingCartService.getOne(queryWrapper);

        //方式二：
        ShoppingCart_购物车 one = shoppingCartService.getOne(
                Wrappers.<ShoppingCart_购物车>lambdaQuery()
                        .eq(ShoppingCart_购物车::getUserId, shoppingCart.getUserId())
                        .eq(shoppingCart.getDishId() != null, ShoppingCart_购物车::getDishId, shoppingCart.getDishId())
                        .eq(shoppingCart.getSetmealId() != null, ShoppingCart_购物车::getSetmealId, shoppingCart.getSetmealId())
        );

        //  查询此菜品或者套餐是否存在，不存在就是第一次买，存在就把数量加1
        if (one != null) {
            //说明已经存在
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //说明第一次买，直接加进去
            shoppingCart.setNumber(1);
            //shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }

        return R.success(one);
    }

    /**
     * 清空购物车(根据userId全删)
     *
     * @return
     */
    @DeleteMapping("/clean")
    private R<String> clean() {
        shoppingCartService.remove(
                Wrappers.<ShoppingCart_购物车>lambdaQuery()
                        .eq(ShoppingCart_购物车::getUserId, BaseContext_id获取.getCurrentId())
        );
        return R.success("清空购物车成功！");
    }

    /**
     * 删除一件商品(分两类，如果数量等于1就删除，大于1就修改数量)
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart_购物车> sub(@RequestBody ShoppingCart_购物车 shoppingCart) {
        //1、设定购物车用户得id  分类是哪个用户添加的商品
        shoppingCart.setUserId(BaseContext_id获取.getCurrentId());


        //2、查询判断个数，如果大于1则数量-1，如果数量等于1，就删除
        ShoppingCart_购物车 one = shoppingCartService.getOne(
                Wrappers.<ShoppingCart_购物车>lambdaQuery()
                        .eq(ShoppingCart_购物车::getUserId, shoppingCart.getUserId())
                        .eq(shoppingCart.getDishId() != null, ShoppingCart_购物车::getDishId, shoppingCart.getDishId())
                        .eq(shoppingCart.getSetmealId() != null, ShoppingCart_购物车::getSetmealId, shoppingCart.getSetmealId())
        );

        //3、判断
        if (one.getNumber() == 1) {
            //数量为1就删除
            one.setNumber(0);
            shoppingCartService.removeById(one);
        } else {
            one.setNumber(one.getNumber() - 1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }
}
